package com.wishwingz.homeworkchat;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.wishwingz.homeworkchat.XMPPManager.OnRosterListener;

public class FriendTabFragment extends SherlockFragment {

	ListView mList;
	ArrayAdapter<User> mAdapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View view = inflater.inflate(R.layout.friends_fragment, container, false);
		
		mList = (ListView)view.findViewById(R.id.friends_list);
		
		XMPPManager.getInstance().getRoster(new OnRosterListener() {
			
			@Override
			public void onRoasterReceived(List<User> users) {
				// TODO Auto-generated method stub
				mAdapter = new ArrayAdapter<User>(getActivity(), android.R.layout.simple_list_item_1,users);
				mList.setAdapter(mAdapter);
			}
		});
		
		mList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				// TODO Auto-generated method stub
				User user = mAdapter.getItem(position);
				Intent i = new Intent(getActivity(), MessageActivity.class);
				i.putExtra(MessageActivity.PARAM_USER, user.user.getUser());
				startActivity(i);
			}
		});
		
		return super.onCreateView(inflater, container, savedInstanceState);
	}

}
